package com.immunology.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.immunology.dao.SurveyDao;
import com.immunology.database.transformers.SurveyTransformer;
import com.immunology.model.ComplaintType;
import com.immunology.model.Survey;

public class SurveyService {

	private static SurveyService surveyService = null;
	private SurveyDao surveyDao = new SurveyDao(new SurveyTransformer());

	private SurveyService() {
	}

	public static SurveyService getInstance() {
		if (surveyService == null) {
			surveyService = new SurveyService();
		}
		return surveyService;
	}

	public void insert(Survey survey) {
		surveyDao.insert(survey);
	}

	public int getLastSurveyId() {
		return surveyDao.getLastSurveyId();
	}

	public Map<String, List<Integer>> getSurveyResultMap(int surveyId) {
		Map<Integer, Integer> complaintIdValueMap = this
				.getcomplaintIdValueMap(surveyId);
		Map<Integer, List<Integer>> complaintTypeIdSumMap = this
				.getComplaintTypeIdSumMap(complaintIdValueMap);
		Map<String, List<Integer>> surveyResultMap = this
				.getResultMap(complaintTypeIdSumMap);
		return surveyResultMap;
	}

	private Map<Integer, Integer> getcomplaintIdValueMap(int surveyId) {
		Map<Integer, Integer> complaintIdValueMap = new HashMap<Integer, Integer>();
		SurveyAnswerService surveyAnswerService = SurveyAnswerService
				.getInstance();
		complaintIdValueMap = surveyAnswerService
				.getComplaintIdValueMapBySurveyId(surveyId);
		return complaintIdValueMap;
	}

	private Map<Integer, List<Integer>> getComplaintTypeIdSumMap(
			Map<Integer, Integer> complaintIdValueMap) {
		Map<Integer, List<Integer>> complaintTypeIdSumMap = new HashMap<Integer, List<Integer>>();
		ComplaintService complaintService = ComplaintService.getInstance();
		Set<Integer> keySet = complaintIdValueMap.keySet();
		for (int complaintId : keySet) {
			int value = complaintIdValueMap.get(complaintId);
			int complaintTypeId = complaintService
					.getComplaintTypeIdByComplaintId(complaintId);
			if (complaintTypeIdSumMap.containsKey(complaintTypeId)) {
				List<Integer> list = complaintTypeIdSumMap.get(complaintTypeId);
				int sum = list.get(0);
				int complaintMaxValue = list.get(1);
				list.clear();
				sum += value;
				list.add(sum);
				list.add(complaintMaxValue + 3);
				complaintTypeIdSumMap.remove(complaintTypeId);
				complaintTypeIdSumMap.put(complaintTypeId, list);
			} else {
				List<Integer> list = new ArrayList<Integer>();
				list.add(value);
				list.add(3);
				complaintTypeIdSumMap.put(complaintTypeId, list);
			}
		}

		return complaintTypeIdSumMap;
	}

	private Map<String, List<Integer>> getResultMap(
			Map<Integer, List<Integer>> complaintTypeIdSumMap) {
		Map<String, List<Integer>> resultMap = new HashMap<String, List<Integer>>();
		ComplaintTypeService complaintTypeService = ComplaintTypeService
				.getInstance();
		Set<Integer> keySet = complaintTypeIdSumMap.keySet();
		for (Integer complaintTypeId : keySet) {
			ComplaintType complaintType = complaintTypeService
					.getComplaintTypeById(complaintTypeId);
			String complaintTypeName = complaintType.getName();
			resultMap.put(complaintTypeName,
					complaintTypeIdSumMap.get(complaintTypeId));
		}
		return resultMap;
	}

	public void setDifficultyDegreeBySurveyId(int surveyId, int difficultyDegree) {
		Survey survey = surveyDao.getById(surveyId);
		survey.setDifficultyDegree(difficultyDegree);
		surveyDao.update(surveyId, survey);
	}

	public List<Survey> getSurveyListByMedicineCardId(int medicineCardId) {
		return surveyDao.getSurveyListByMedicineCardId(medicineCardId);
	}

}
